package com.atguigu.phoenix;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * @author WEIYUNHUI
 * @date 2023/7/1 15:46
 *
 * Phoenix使用的是标准的JDBC。
 *    注册驱动 创建连接  编写SQL  编译SQL  设置参数  执行SQL  处理结果  关闭连接
 */
public class PhoenixDemo {
    public static void main(String[] args) {
        testSelect();

    }

    public static void testSelect(){
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
            String url = "jdbc:phoenix:hadoop102,hadoop103,hadoop104:2181" ;
            Properties props = new Properties();
            props.setProperty("phoenix.schema.isNamespaceMappingEnabled", "true");
            Connection connection = DriverManager.getConnection(url,props);
            String sql = "select id, name, age from t2 where id = ?" ;
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1 ,"1001" );
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                String id  = resultSet.getString("id");
                String name = resultSet.getString("name");
                String age = resultSet.getString("age");
                System.out.println(id + " : " + name + " : " + age );
            }
            resultSet.close();
            preparedStatement.close();
            connection.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     *  作业: 完成 新增、 修改、 删除
     *
     *  事务自动提交: connection.setAutoCommit(true);
     *
     *  提交事务： connection.commit();
     *
     *  回滚事务: connection.rollback();
     *
     */
    public static void testUpsert(){}

    public static void testDelete(){}
}
